<style>
.pure_table_academic { width: 100%; margin-top: 5px; }
.pure_table_academic th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_academic img { cursor: pointer; margin-right: 2px; }
.pure_table_academic td { vertical-align: middle; height: 30px; }
.pure_table_academic td input { width:90%; border: 1px solid #ddd; padding: 5px; border-radius: 5px; }
.academic_program { width: 140px; height: 250px; padding: 10px; display: inline-block; margin: 0 10px 10px 0; text-align: center; }
.academic_program h3 { height: 45px; overflow: hidden; }
.academic_program div.period { color: #000; font-size: 11px !important; font-family: Tahoma !important; padding-bottom: 10px; }
.academic_program textarea { font-family: Lucida Sans Unicode !important; }
.pure-button-program { color: #fff !important; background: rgb(28, 184, 65) !important; }
.pure-button-intake { color: #fff !important; background: rgb(223, 117, 20) !important; }
.pure-button-back { color: #666 !important; background: #eee !important; }
.pure-button-enrl { color: #fff !important; background: #ebc85e !important; }
.pure-button-pass { color: #fff !important; background: rgb(28, 184, 65) !important; }
.pure-button-fail { color: #fff !important; background: #ff0000 !important; }
.pure_form_academic { width: 100%; border: 1px solid #ddd; border-radius: 3px; padding: 0 0 10px 0; background: #FFFFEE; }
.pure_form_academic h3 { padding-left: 15px; }
#ep_navbar ul { margin: 0; padding: 0; list-style-type: none; text-align: left; }
#ep_navbar ul li { display: inline; }
#ep_navbar ul li i { padding: 0 10px; }
#ep_navbar ul li a { font-size: 13px !important; text-decoration: none; padding: 5px 10px; color: #fff; background-color: #2BBBD8; }
#ep_navbar ul li a:hover { color: #fff; background-color: #369; }
</style>

<?php 
$date = date('Y-m-d', time());
$student_id = Utils::get_param('id', 0);

$user = json_decode($_SESSION['admin_user']);

$opts = new stdClass();
$opts->cd = 't.student_id = :v1 AND t.is_paid = :v2 AND t.is_cancel = 0 AND t.is_withdrawn = 0';
$opts->cdv = array(':v1' => $student_id, ':v2' => 1);
$pdenrls = $orm->get('pb_colg_enrollment', 't.id', 'DESC', NULL, NULL, $opts)->d;

$courses = $orm->get('pb_colg_course', NULL, NULL, NULL, NULL, NULL)->d;

$opts = new stdClass();
$opts->cd = 't.student_id = :v1';
$opts->cdv = array(':v1' => $student_id);
$opts->sel = 't.*, t1.times AS __times';
$opts->jt = 'LEFT JOIN pb_colg_class AS t1 ON t1.id = t.class_id';
$enrlclasses = $orm->get('pb_colg_enrolled_class', 't.course_name', 'ASC', NULL, NULL, $opts)->d;

$opts = new stdClass();
$opts->cd = 't.student_id = :v1';
$opts->cdv = array(':v1' => $student_id);
$enrlintakes = $orm->get('pb_colg_enrolled_intake', 't.start', 'DESC', NULL, NULL, $opts)->d;

$opts = new stdClass();
$opts->cd = 't.student_id = :v1';
$opts->cdv = array(':v1' => $student_id);
$opts->sel = 't.*, t1.reqs AS __reqs';
$opts->jt = 'LEFT JOIN pb_colg_program AS t1 ON t1.id = t.program_id';
$enrlprograms = $orm->get('pb_colg_enrolled_program', 't.program_type', 'ASC', NULL, NULL, $opts)->d;

$programs = $orm->get('pb_colg_program', 't.name', 'ASC', NULL, NULL, NULL)->d;

foreach ($enrlprograms as $itm) {
  $itm->__reqs = json_decode($itm->__reqs);
  
  $itm->__enrlintakes = array();
  foreach ($enrlintakes as $itm2) {
    if ($itm2->enrolled_program_id == $itm->id) {
      $itm2->__enrlclasses = array();
      foreach ($enrlclasses as $itm3) {
        $itm3->__atdn = ColgUtils::atdn_by_enrolled_class($orm, $itm3->id);
        if ($itm3->enrolled_intake_id == $itm2->id) {
          $itm2->__enrlclasses[] = $itm3;
        }
      }
      $itm->__enrlintakes[] = $itm2;
    }
  }
  foreach ($pdenrls as $itm2) {
    if ($itm2->enrolled_program_id == $itm->id) {
      if (!isset($itm->__start)) {
        $itm->__start = $itm2->start_academic;
      } else {
        $itm->__start = (strtotime($itm->__start) > strtotime($itm2->start_academic)) ? $itm2->start_academic : $itm->__start;
      }
      if (!isset($itm->__end)) {
        $itm->__end = $itm2->end_academic;
      } else {
        $itm->__end = (strtotime($itm->__end) < strtotime($itm2->end_academic)) ? $itm2->end_academic : $itm->__end;
      }
    }
  }
  if ($itm->program_type == PT_LANGUAGE && isset($itm->__start) && isset($itm->__end)) {
    $past = strtotime('now') - strtotime($itm->__start);
    $left = strtotime($itm->__end) - strtotime('now');
    $itm->progress__ = ($past / ($past + $left)) * 100;
    $orm->update('pb_colg_enrolled_program', $itm);
  }
}

$nceats = unserialize(NCEA_TS);
?>

<div id="academic_content"></div>
<div id="dialog_academic" class="pure-form" style="display: none;"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __nceats = JSON.parse('<?php echo addslashes(json_encode($nceats)); ?>');
  var __courses = JSON.parse('<?php echo addslashes(json_encode($courses)); ?>');
  var __pdenrls = JSON.parse('<?php echo addslashes(json_encode($pdenrls)); ?>');
  var __enrlclasses = JSON.parse('<?php echo addslashes(json_encode($enrlclasses)); ?>');
  var __enrlintakes = JSON.parse('<?php echo addslashes(json_encode($enrlintakes)); ?>');
  var __enrlprograms = JSON.parse('<?php echo addslashes(json_encode($enrlprograms)); ?>');
  var __programs = JSON.parse('<?php echo addslashes(json_encode($programs)); ?>');
  var __date = '<?php echo $date; ?>';
  var __diploma = '<?php echo PT_DIPLOMA; ?>';
  var __language = '<?php echo PT_LANGUAGE; ?>';
  var __ncea = '<?php echo PT_NCEA; ?>';
  var __intakes = [];
  var __newenrlintakes = [];
  var __dip_m, __lang_m, __ncea_m;
  
  function render_academic() {
    $('#academic_content').empty();
    for (var idx in __enrlprograms) {
      var itm = __enrlprograms[idx];
      __render_enrl_program(itm);
    }

    $('.knob').each(function () {
      var $this = $(this);
      var myVal = $this.attr("rel");
      // alert(myVal);
      $this.knob({
        draw: function () {
          $(this.i).val(this.cv + '%\nCompleted') //Puts a percent after values
        }
      });
      $({
        value: 0
      }).animate({
        value: myVal
      }, {
        duration: 2000,
        easing: 'swing',
        step: function () {
          $this.val(Math.ceil(this.value)).trigger('change');
        }
      })
    });
  };

  function __render_enrl_program(obj) {
    $('#academic_content').append(
      '<div class="ui-state-highlight ui-corner-all academic_program">' + 
        '<span style="float: right; color: red;" onclick="delete_enrlprogram(' + obj.id + ');"><i class="fa fa-times"></i></span>' +
        '<h3 title="' + obj.program_name + '">' + Utils.get_short(obj.program_name, 45) + '</h3>' + 
        '<div class="period">' + obj.__start + ' ~ ' + obj.__end + '</div>' +
        '<textarea class="knob" rel="' + obj.progress__ + '" data-angleOffset=-125 data-angleArc=250 data-width="100%" data-readOnly=true value="0" style="resize: none;"></textarea>' +
        '<a onclick="__set_enrl_program(' + obj.id + ', -1);" class="pure-button pure-button-program" href="#" style="margin-left: auto;"><i class="fa fa-cog"></i>&nbsp;Manage</a>' +
      '</div>'
    );
  };

  function delete_enrlprogram(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'delete_enrlprogram',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        Utils.splice_by_id(id, __enrlprograms);
        render_academic();
      },
      { confirm: 1 }
    );
  };

  function __set_enrl_program(id, eit_id) {
    $('#academic_content').html(
      '<table style="width: 100%;" cellpadding=0 cellspacing=0>' +
        '<tr>' +
          '<td>' +
            '<div id="ep_navbar"></div>' +
            '<div">&nbsp;</div>' +
            '<div id="ep_content" style="min-width: 700px;"></div>' +
          '</td>' +
          '<td  rowspan=2 style="width: 200px;">' + 
            '<div class="pure-form pure_form_academic">' +
              '<h3>Programs</h3>' +
              '<div id="ep_rightbar" ></div>' +
            '</div>' +
            '<div">&nbsp;</div>' +
            '<div id="ep_extra"></div>' +
          '</td>' +
        '<tr>' +
      '</table>'
    );
    var obj = Utils.get_by_id(id, __enrlprograms);
    if (obj.program_type == __diploma) {
      __dip_m = new DipMgmt(obj, {
        student: __data[0],
        pdenrls: __pdenrls,
        enrlclasses: __enrlclasses,
        enrlintakes: __enrlintakes,
        courses: __courses,
        update_nav: __update_nav,
        readonly: (__user.role == 'Admin' || __user.role == 'Mgmt') ?  0 : 1,
        programs: __programs
      });
      if (eit_id != -1) {
        __dip_m.create_eit(eit_id);
      } else {
        __dip_m.create();
      }
    } else if (obj.program_type == __language) {
      __lang_m = new LangMgmt(obj, {
        date: __date,
        student: __data[0],
        pdenrls: __pdenrls,
        enrlclasses: __enrlclasses,
        enrlintakes: __enrlintakes,
        courses: __courses,
        update_nav: __update_nav,
        readonly: (__user.role == 'Admin' || __user.role == 'Mgmt') ?  0 : 1,
        programs: __programs
      });
      if (eit_id != -1) {
        __lang_m.create_eit(eit_id);
      } else {
        __lang_m.create();
      }
    } else if (obj.program_type == __ncea) {
      __ncea_m = new NceaMgmt(obj, {
        nceats: __nceats,
        date: __date,
        student: __data[0],
        pdenrls: __pdenrls,
        enrlclasses: __enrlclasses,
        enrlintakes: __enrlintakes,
        courses: __courses,
        update_nav: __update_nav,
        readonly: (__user.role == 'Admin' || __user.role == 'Mgmt') ?  0 : 1,
        programs: __programs
      });
      if (eit_id != -1) {
        __ncea_m.create_eit(eit_id);
      } else {
        __ncea_m.create();
      }
    }
  };

  function __update_nav(id, eit_id) {
    __set_rightbar(id, eit_id);
    __set_navbar(id, eit_id);
  };

  function __set_navbar(id, eit_id) {
    var obj = Utils.get_by_id(id, __enrlprograms);
    var obj2 = Utils.get_by_id(eit_id, __enrlintakes);
    $("#ep_navbar").empty();
    $('#ep_navbar').append(
      '<div class="pure-form"><select id="enrl_program_change"></select></div>' +
      '<br/>' +
      '<ul id="pgul_0">' +
        '<li><a href="#" onclick="render_academic(); return false;">All Programs</a></li>' +
        '<li><i class="fa fa-arrow-circle-right"></i></li>' +
        '<li title="' + obj.program_name + '"><a href="#" onclick="__set_enrl_program(' + id + ', -1); return false;">' + Utils.get_short(obj.program_name, 20) +'</a></li>' +
        ((obj2) ? '<li><i class="fa fa-arrow-circle-right"></i></li>' : '') +
        ((obj2) ? '<li title="' + obj2.start + ' to ' + obj2.end + '"><a href="#" onclick="__set_enrl_program(' + id + ', ' + eit_id + '); return false;">' + obj2.start + ' to ' + obj2.end + '</a></li>' : '') +
      '</ul>'
    );

    if (__user.role == 'Admin' || __user.role == 'Mgmt') {
      $("#enrl_program_change").show();
      
      var enrlprogram = Utils.get_by_id(id, __enrlprograms);
      for (var idx in __programs) {
        var itm = __programs[idx];
        if (itm.type != enrlprogram.program_type) {
          continue;
        }
        $("#enrl_program_change").append('<option value="' + itm.id + '" ' + ((itm.id == enrlprogram.program_id) ? 'selected="selected" ' : '') + '>' + itm.name + '</option>');
      }
      $("#enrl_program_change").on('change', function() {
        var program = Utils.get_by_id($("#enrl_program_change :selected").val(), __programs);
        enrlprogram.program_id = program.id;
        enrlprogram.program_name = program.name;
        enrlprogram.program_type = program.type;
        enrlprogram.__reqs = JSON.parse(program.reqs);
        Ajax.request(
          __requrl + 'submit_enrl_program',
          'req=' + Utils.encodeURI(JSON.stringify(enrlprogram)),
          function(rslt) {
            __set_enrl_program(id, eit_id);
          },
          { confirm: 1 }
        );
      });
    } else {
      $("#enrl_program_change").hide();
    }
  };

  function __set_rightbar(id, eit_id) {
    var uid = Utils.unique();
    $("#ep_rightbar").empty();
    $('#ep_rightbar').append('<ul id="pgul_' + uid + '"></ul>');
    
    for (var idx in __enrlprograms) {
      var itm = __enrlprograms[idx];
      $('#pgul_' + uid).append(
        '<li rel="-1" value="' + itm.id + '">' +
          '<a href="#"' + ((id == itm.id && eit_id == -1) ? ' class="jstree-clicked"' : '') + ' title="' + itm.program_name + '" value="' + itm.id + '">' + Utils.get_short(itm.program_name, 20) + '</a>' +
          ((itm.__enrlintakes.length > 0) ? '<ul id="itul_' + itm.id + '"></ul>' : '') + 
        '</li>'
      );
      for (var idx2 in itm.__enrlintakes) {
        var itm2 = itm.__enrlintakes[idx2];
        $('#itul_' + itm.id).append(
          '<li rel="' + itm2.id + '" value="' + itm.id + '">' +
            '<a href="#" ' + ((eit_id == itm2.id) ? ' style="background:#beebff; border:1px solid #99defd; padding:0 2px 0 1px;"' : '') + ' value="' + itm.id + '">' + itm2.start + ' to ' + itm2.end + '</a>' +
          '</li>'
        );
        
      }
    }
    $("#ep_rightbar").jstree({ "plugins" : ["themes", "html_data", "ui"] })
    .on('loaded.jstree', function() {
      $("#ep_rightbar").jstree('open_all');
    })
    .bind("select_node.jstree", function (event, data) { 
      val = data.rslt.obj.attr("value");
      rel = data.rslt.obj.attr("rel");
      __set_enrl_program(val, rel)
    })
    .delegate("a", "click", function (event, data) { event.preventDefault(); });
  };
  /*]]>*/
</script>